#include "widget.h"
#include "ui_widget.h"
#include <QtDebug>
#include <QString>
#include <QRegExpValidator>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

   // ui->pushButton->setEnabled(false);
    ui->label_5->setText("密码为空");
    //初始化name LineEdit
    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);

    //初始化password LineEdit
    ui->lineEdit_password->setPlaceholderText("请输入密码");
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);

    ui->lineEdit_password_ensure->setPlaceholderText("请输入确认密码");
    ui->lineEdit_password_ensure->setClearButtonEnabled(true);
    ui->lineEdit_password_ensure->setEchoMode(QLineEdit::Password);
    //初始化tel LineEdit
    //创建正则表达式
    QRegExp regexp("^1\\d{10}$");
    //注册验证器
    ui->lineEdit_tel->setValidator(new QRegExpValidator(regexp));
    ui->lineEdit_tel->setPlaceholderText("请输入联系方式");
    //ui->lineEdit_tel->setInputMask("000-0000-0000");
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString sex = ui->radioButton_man->isChecked()?"man":"female";

    qDebug() << ui->lineEdit_name->text()
             << ui->lineEdit_password->text()
             << sex
             <<ui->lineEdit_tel->text();

}

void Widget::on_lineEdit_tel_textEdited(const QString &text)
{
    QString content = text;
    int pos = 0;
    if(ui->lineEdit_tel->validator()->validate(content,pos) == QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }
}

void Widget::on_lineEdit_password_ensure_textEdited(const QString &arg1)
{
        (void)arg1;
        QString p1 = ui->lineEdit_password->text();
        QString p2 = ui->lineEdit_password_ensure->text();

        if(p1.isEmpty() && p2.isEmpty())
             ui->label_5->setText("密码为空");
        if(p1 == p2)
            ui->label_5->setText("密码一致");
        else
            ui->label_5->setText("密码不一致");
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        //显示密码
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_password_ensure->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        //隐藏密码
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
        ui->lineEdit_password_ensure->setEchoMode(QLineEdit::Password);
    }
}

void Widget::on_textEdit_textChanged()
{
    //
//    const QString& text = ui->textEdit->toPlainText();
//    ui->label_6->setText(text);
    qDebug() << "textChanged "<< ui->textEdit->toPlainText();
}

void Widget::on_textEdit_selectionChanged()
{
    QTextCursor cursor =ui->textEdit->textCursor();
    qDebug() << "selectionChanged: "<< cursor.selectedText();
}

void Widget::on_textEdit_cursorPositionChanged()
{
    QTextCursor cursor = ui->textEdit->textCursor();
    qDebug() << "cursorPositionChanged: "<<cursor.position();
}

void Widget::on_textEdit_undoAvailable(bool b)
{
    qDebug() << "undoAvailable: "<< b;
}

void Widget::on_textEdit_redoAvailable(bool b)
{
    qDebug() << "redoAvailable: "<< b;
}

void Widget::on_textEdit_copyAvailable(bool b)
{
    qDebug() << "copyAvailable: "<< b;
}
